package com.dgut.ibeacon.controller;


import com.dgut.ibeacon.entity.AjaxResult;
import com.dgut.ibeacon.entity.Bookshelf;
import com.dgut.ibeacon.service.IBookshelfService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuec
 * @since 2020-10-14
 */
@Controller
@RequestMapping("//bookshelf")
public class BookshelfController {

    @Autowired
    IBookshelfService bookshelfService;

    @GetMapping("/addBookShelfs")
    public String addBookShelfs(){
        return "addBookShelfs";
    }

    @GetMapping("/checkBookShelfs")
    public String checkBookShelfs(){
        return "checkBookShelfs";
    }


    /**
     * 创建书架
     * @param bookshelf
     * @return
     */
    @RequiresRoles("super_admin")
    @ResponseBody
    @RequestMapping("/createBookshelf")
    public AjaxResult createBookshelf(Bookshelf bookshelf) {
        if (bookshelfService.save(bookshelf) == true) {
            return AjaxResult.success("新书架创建成功");
        } else {
            return AjaxResult.error("书架创建失败");
        }
    }

    /**
     * 删除书架
     * @param bookshelfId
     * @return
     */
    @RequiresRoles("super_admin")
    @ResponseBody
    @RequestMapping("/deleteBookshelf")
    public AjaxResult deleteBookshelf(int bookshelfId) {
        Bookshelf bookshelf = bookshelfService.getById(bookshelfId);
        if (bookshelf == null) {
            return AjaxResult.error("书架不存在");
        }
        if (bookshelfService.removeById(bookshelfId) == true) {
            return AjaxResult.success("书架名称:" + bookshelf.getBookshelfClassname() + ",已删除");
        } else {
            return AjaxResult.error("书架删除失败");
        }
    }

    /**
     * 修改书架评论区的开放状态
     * @param bookshelfId
     * @return
     */
    @RequiresRoles("super_admin")
    @ResponseBody
    @RequestMapping("/alterCommentStatus")
    public AjaxResult alterCommentStatus(int bookshelfId) {
        Bookshelf bookshelf = bookshelfService.getById(bookshelfId);
        if (bookshelf == null) {
            return AjaxResult.error("书架不存在");
        }
        Bookshelf alter = new Bookshelf();
        alter.setBookshelfId(bookshelfId);
        alter.setIfOpenComment(bookshelf.getIfOpenComment() == true ? false : true);
        if (bookshelfService.updateById(alter)) {
            return AjaxResult.success("书架评论区开放状态已修改");
        } else {
            return AjaxResult.error("书架评论区开放状态修改失败！！！");
        }
    }

    /**
     * 修改书架信息
     * @param bookshelf
     * @return
     */
    @RequiresRoles("super_admin")
    @ResponseBody
    @RequestMapping("/alterInfo")
    public AjaxResult alterInfo(Bookshelf bookshelf) {
        Bookshelf bookshelfDB = bookshelfService.getById(bookshelf.getBookshelfId());
        if (bookshelfDB == null) {
            return AjaxResult.error("书架不存在");
        }
        bookshelfDB.setGmtModified(null);  //置空才能更改
        if (bookshelfService.updateById(bookshelfDB)) {
            return AjaxResult.success("书架信息已修改");
        } else {
            return AjaxResult.error("书架信息修改失败！！！");
        }
    }

    /**
     * 列出图书馆所有的书架
     * @return
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public AjaxResult listAll() {
       List list=bookshelfService.list();
        if (list.size()==1) {
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error("尚未录入书架！！！");
        }
    }

    /**
     * 根据楼层序号查找书架
     * @param layer
     * @return
     */
    @ResponseBody
    @RequestMapping("/listByLayer")
    public AjaxResult listByLayer(int layer) {
        Map map=new HashMap();
        map.put("bookshelf_layer",layer);
        List list=bookshelfService.listByMap(map);
        if (list.size()==1) {
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error("尚未录入书架！！！");
        }
    }

}
